package com.talent.web.config.minio;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import io.minio.*;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.XmlParserException;
import io.minio.http.Method;
import io.minio.messages.Part;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName：TlMinioClient
 * @Author: hszhou
 * @Date: 2025/3/29 16:43
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class TlMinioClient extends MinioAsyncClient {

    public TlMinioClient(MinioAsyncClient   client) {
        super(client);
    }

    /**
     * 判断文件是否存在
     *
     * @param bucketName 存储桶
     * @param objectName 对象
     * @return true:存在 false:失败
     */
    @SneakyThrows
    public boolean doesObjectExist(String bucketName, String objectName) {
        boolean isExist = true;
        try {
            super.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()).get();
        } catch (Exception e) {
            isExist = false;
        }
        return isExist;
    }
    /**
     * 初始化分片上传、获取 uploadId
     *
     * @param bucket           String  存储桶名称
     *
     * @param object           String   文件名称
     * @param contentType          Multimap<String, String> 请求头
     * @return String
     */
    @SneakyThrows
    public String getUploadId(String bucket ,String object,String contentType)  {
        Multimap<String, String> headers = HashMultimap.create();
        headers.put("Content-Type", contentType);
        return super.createMultipartUploadAsync(bucket, null, object, headers, null).get().result().uploadId();
    }

    public void UploadPart(String bucketName, String objectName, Object file, long size, String uploadId, int partNumber,String contentType) throws InsufficientDataException, IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException, InternalException {
        Multimap<String, String> headers = HashMultimap.create();
        headers.put("Content-Type", contentType);
        super.uploadPartAsync(bucketName, null, objectName, file, size, uploadId, partNumber, headers, null);
    }

    /**
     * 分片上传的预签名上传地址  过期时间为1天 get请求url
     * @param bucketName  桶名
     * @param filePath    Oss文件路径
     * @param queryParams 查询参数
     * @return 分片上传的预签名上传地址
     */
    @SneakyThrows
    public String getPresignedObjectUrl(String bucketName, String filePath, Map<String, String> queryParams) {
        return super.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(filePath)
                        .expiry(1, TimeUnit.DAYS)
                        .extraQueryParams(queryParams)
                        .build());
    }
    /**
     * 合并分片
     *
     * @param bucketName       String   桶名称

     * @param objectName       String   文件名称
     * @param uploadId         String   上传的 uploadId
     * @param parts            Part[]   分片集合
     * @param contentType     Multimap<String, String>
     * @return ObjectWriteResponse
     */
    @SneakyThrows
    public ObjectWriteResponse completeMultipartUpload(String bucketName, String objectName, String uploadId, Part[] parts,String contentType)  {
        Multimap<String, String> headers = HashMultimap.create();
        headers.put("Content-Type", contentType);

        return super.completeMultipartUploadAsync(bucketName, null, objectName, uploadId, parts, headers, null).get();
    }


    /**
     * 查询当前上传后的分片信息
     *
     * @param bucketName       String   桶名称
     * @param objectName       String   文件名称
     * @param uploadId         String   上传的 uploadId
     * @return ListPartsResponse
     */
    @SneakyThrows
    public ListPartsResponse listMultipart(String bucketName, String objectName,  String uploadId)  {
        return super.listPartsAsync(bucketName, null, objectName, 1000, 0, uploadId, null, null).get();
    }


}
